<template>
  <div class="login-container">
    <el-card class="logincenter">
      <!-- 登录页头像 -->
      <div class="loginhead"></div>
      <!-- 登录表单 -->
      <el-form
        ref="ruleallform"
        center
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
            placeholder="请输入账户"
            name="username"
            type="text"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            placeholder="请输入密码"
            name="password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="captchaValue">
          <el-col :span="14">
            <el-input
              v-model="loginForm.captchaValue"
              placeholder="请输入验证码"
              name="captchaValue"
              type="text"
            >
              <i
                slot="prefix"
                class="el-icon-circle-check"
                :style="{ color: yzmcolor }"
              ></i>
            </el-input>
          </el-col>
          <el-col :span="10" class="moveyzm">
            <img :src="yzm.captchaImg" @click="refreshyzm" />
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button
            :loading="loading"
            style="width: 100%"
            type="primary"
            @click="handleLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { yanzhengma } from "@/api/user";
import { mapActions } from "vuex";
export default {
  data() {
    // 自定义验证规则
    let validateCaptchaValue = (rule, value, callback) => {
      // value.length !== 4 ? callback(new Error("验证码位数错误!")) : callback();
      if (value.length !== 4) {
        if (value == "") {
          callback(new Error("验证码不能为空!"));
        } else {
          callback(new Error("验证码位数错误!"));
        }
        this.yzmcolor = "red";
      } else {
        this.yzmcolor = "green";
        callback();
      }
    };
    return {
      //用户登录信息
      loginForm: {
        username: "admin",
        password: "12345",
        captchaValue: "",
        captchaId: "",
      },
      // 验证码判断颜色
      yzmcolor: "",
      // 验证规则
      loginRules: {
        username: [
          { required: true, message: "账户不能为空!", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空!", trigger: "blur" },
        ],
        captchaValue: [
          // { required: true, message: "验证码不能为空!", trigger: "blur" },
          { validator: validateCaptchaValue, trigger: "blur" },
        ],
      },
      // 存储验证码
      yzm: {
        captchaId: "",
        captchaImg: "",
      },
      loading: false,
    };
  },
  created() {
    // 预先调用一个方法
    this.yanzhengma();
  },
  methods: {
    ...mapActions(["user/login"]),
    // 验证码请求
    async yanzhengma() {
      try {
        const res = await yanzhengma();
        // console.log(res);
        this.yzm = { ...res };
        this.loginForm.captchaId = this.yzm.captchaId;
      } catch (error) {
        console.log(error);
      }
    },
    // 刷新验证码
    refreshyzm() {
      this.yanzhengma();
    },
    // 登录
    handleLogin() {
      // 自动校验
      this.$refs.ruleallform.validate(async (isOk) => {
        if (isOk) {
          try {
            this.loading = true;
            await this["user/login"](this.loginForm);
            // 登录页面跳转
            this.$router.push("/");
          } catch (error) {
            console.log(error);
          } finally {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style scoped lang='scss'>
.login-container {
  height: 100vh;
  width: 100%;
  background-image: url("@/assets/back.png");
  background-color: rgb(238, 239, 236);
  background-repeat: no-repeat;
  background-size: 100% 60%;
  display: flex;
  justify-content: center;
  align-items: center;
  .logincenter {
    width: 380px;
    height: 380px;
    background-color: white;
    border-radius: 15px;
  }
}
.loginhead {
  width: 80px;
  height: 80px;
  border-radius: 5px;
  background-image: url("@/assets/login.jpg");
  background-size: contain;
  background-repeat: no-repeat;
  margin: 10px auto;
}
.moveyzm {
  cursor: pointer;
  height: 40px;
}
</style>